﻿using GFluentCode.CodeMaker.Template;
using GFluentCode.ORM.SqlServer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace GFluentCode.CodeMaker
{
    public partial class FrmWebApi : Form
    {        
        string _savePath;

        Assembly ass;

        public FrmWebApi(string savePath)
        {
            InitializeComponent();
            
            _savePath = savePath;

            Load += (s, e) =>
            {
                TabApi.TabPages.Clear();

                LbOrm.Items.Clear();
            };
        }

        private void BtnOpenFile_Click(object sender, EventArgs e)
        {
            var dr = OpenFile.ShowDialog();
            if (dr != DialogResult.OK) return;

            var path = OpenFile.FileName;

            TxtOrmDllPath.Text = path;

            var apiNS = TxtApiNS.Text;
            var apiBase = TxtApiBase.Text;
            var entityNS = TxtEntityNS.Text;
            var apiBaseNS = TxtApiBaseNS.Text;

            ass = Assembly.LoadFrom(path);

            //类
            foreach (Type type in ass.GetTypes())
            {
                LbOrm.Items.Add(type.Name);
                var str = WebApiTemplate.ToApiTemplate(type, apiNS, apiBaseNS, entityNS, apiBase);
                CreateTabPage(type.Name.Replace("ORM","Controller"), str);
            }
        }

        void CreateTabPage(string name,string str)
        {
            var tp = new TabPage(name);
            var txt = new TextBox { ScrollBars = ScrollBars.Both ,Multiline=true,Dock=DockStyle.Fill,Text=str};

            tp.Controls.Add(txt);
            TabApi.TabPages.Add(tp);
        }
    }
}
